package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.entity.PageResult;
import com.usian.entity.QueryPageBean;

import com.usian.mapper.GoodsMapper;
import com.usian.pojo.Goods;
import com.usian.pojo.Role;
import com.usian.pojo.User;
import com.usian.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public PageResult pageList(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        HashMap queryString = queryPageBean.getQueryString();

        Integer roleId = (Integer) queryString.get("roleId");
        List<Goods> goods = goodsMapper.pageList(queryString);
        List<Goods> goodsNow = new ArrayList<>();
        for (Goods good : goods) {
            if (roleId == 1) {
                if (good.getStatus() == 3 || good.getStatus() == 4 || good.getStatus() == 5) {
                    goodsNow.add(good);
                }
            }
            if (roleId == 2) {
                if (good.getStatus() == 0 || good.getStatus() == 1 || good.getStatus() == 2 || good.getStatus() == 6 || good.getStatus() == 7) {
                    goodsNow.add(good);
                }
            }
        }

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsNow);
        return new PageResult<>(goodsPageInfo.getTotal(), goodsPageInfo.getList());
    }

    @Override
    public List<User> showList(User user) {
        return goodsMapper.showList(user);

    }

    @Override
    public List<Role> findRole() {
        return goodsMapper.findRole();
    }

    @Override
    public void delete(Integer id) {
        goodsMapper.delete(id);
    }

    @Override
    public void add(Goods goods) {
        goodsMapper.add(goods);
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.update(goods);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        goodsMapper.updateStatus(id,status);
    }

    @Override
    public void register(User user) {
        goodsMapper.register(user);
    }
}
